package com.youdoneed.community.forum.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.youdoneed.community.forum.vo.CommentVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.youdoneed.community.common.vo.PageData;
import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.forum.vo.CreateCommentVo;
import com.youdoneed.community.forum.entity.Forumcomment;
import com.youdoneed.community.forum.service.ForumcommentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 论坛评论模块控制器
 *
 * @author lid
 * @date 2017.6.1
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/forumcomment", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "论坛评论模块控制器", description = "论坛评论模块相关操作")
public class ForumcommentController {

    private static final Logger logger = LoggerFactory.getLogger(ForumcommentController.class);

    @Autowired
    ForumcommentService forumcommentService;

    /**
     * 获取我的朋友对新闻的评论
     *
     * @param token
     * @param postId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getFrindsCommentInNews", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "获取我的朋友对新闻的评论", value = "获取我的朋友对新闻的评论")
    public ResultMessage<List<Forumcomment>> getFrindsCommentInNews(@RequestHeader("Authorization") String token, String postId) {
        List<Forumcomment> listForumcomment = forumcommentService.getFrindsCommentInNews(postId);
        return ResultMessage.newSuccess("获取我的朋友对新闻的评论成功！").setData(listForumcomment);

    }

    /**
     * 用户发表评论接口
     *
     * @param vo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/publishComment", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "用户发表评论", value = "用户发表评论")
    public ResultMessage<?> publishComment(@RequestBody CreateCommentVo vo) {
        if (forumcommentService.publishForumcomment(vo)) {
            return ResultMessage.newSuccess("用户评论成功！");
        }
        return ResultMessage.newFailure("用户评论失败！");
    }

    /**
     * 通过id查找评论
     *
     * @param id
     * @return Forumcomment
     * @author lid
     * @date 2017.6.6
     */
    @RequestMapping(value = "/getForumcommentById", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "通过id查找评论", value = "通过id查找评论")
    public ResultMessage<Forumcomment> getForumcommentById(String id) {
        logger.info("通过id查找评论!id:" + id);
        Forumcomment forumcomment = forumcommentService.selectById(id);
        return ResultMessage.newSuccess("通过id查找评论成功").setData(forumcomment);
    }

    /**
     * 通过帖子id或者新闻id查找评论
     *
     * @param forumMsgId
     * @return List<Forumcomment>
     * @author lid
     * @date 2017.6.12
     */
    @RequestMapping(value = "/getForumcommentByForumMsgId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "通过论坛帖子id查找评论", value = "通过论坛帖子id查找评论")
    public ResultMessage<PageData> getForumcommentByForumMsgId(String userId, String forumMsgId, Integer limit, Integer offset) {
        logger.info("通过论坛帖子id查找评论!id:" + forumMsgId);
        PageData pageData = forumcommentService.getCommentByMsgId(userId, forumMsgId, limit, offset);
        return ResultMessage.newSuccess("通过论坛帖子id查找评论成功！").setData(pageData);
    }


    /**
     * 根据评论id删除评论
     *
     * @param id
     * @return ResultMessage
     * @author lid
     * @date 2017.6.6
     */
    @RequestMapping(value = "/deleteById", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "删除帖子评论", value = "删除帖子评论")
    public ResultMessage<?> deleteById(String id) {
        logger.info("通过论坛帖子id查找评论!id:" + id);
        forumcommentService.deleteById(id);
        return new ResultMessage<>();
    }

    /**
     * 根据条件查询帖子评论
     *
     * @param content
     * @param createDateS
     * @param createDateE
     * @param limit
     * @param offset
     * @return ResultMessage
     * @author lixs
     * @date 2017.6.12
     */
    @RequestMapping(value = "/getSelectForumcomment", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台查询帖子评论", value = "后台查询帖子评论")
    public ResultMessage<PageData> getSelectForumcomment(String content, String createDateS, String createDateE, Integer limit, Integer offset) {
        logger.info("内容" + content + ",时间段" + createDateS + "-" + createDateE + "查询论坛帖子评论");
        List<Forumcomment> commentList = forumcommentService.getSelectForumcomment(content, createDateS, createDateE);
        List<Forumcomment> forumCommentList = forumcommentService.getSelectForumcommentByPage(content, createDateS, createDateE, limit, offset);
        if (null == forumCommentList) {
            return ResultMessage.newFailure("获取评论失败！");
        }
        PageData page = new PageData();
        page.setRows(forumCommentList);
        page.setTotal(commentList.size());
        return ResultMessage.newSuccess("获取帖子评论成功！").setData(page);
    }
}
